package cn.tedu.file;

import java.io.*;

/**本类用于测试字符流的读取*/
public class TestIn2 {
    public static void main(String[] args) {
        method1();//普通字符输入流
        method2();//高效字符输入流

    }

    private static void method2(){
        //1.定义一个本方法中都生效的局部变量，手动初始化值为null
        Reader in = null;
        //2.输入高效字符输入流BufferedReader流对象
        try {
            in = new BufferedReader(new FileReader(new File("F:\\ready\\1.txt")));
            in = new BufferedReader((new FileReader("F:\\ready\\1.txt")));

            //3.使用流对象
            int b;
            while ((b = in.read()) != -1){
                System.out.println(b);
            }
            //这样会产生一种跳着读的效果，会遗漏数据，因为in.read()出现一次执行一次，一个while(){}循环结束，in.read()执行两次
//            while(in.read() != -1){
//                System.out.println(in.read());
//            }
//            System.out.println(in.read());
//            System.out.println(in.read());

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private static void method1() {
        //1。定义一个在本方法中都生效的局部变量，手动初始化值为null
        Reader in = null;
        try {
            //2.创建流对象
            in = new FileReader(new File("F:\\ready\\1.txt"));
            in = new FileReader("F:\\ready\\1.txt");

            //3.使用流对象进行读取
            int b;
            while((b = in.read()) != -1){
                System.out.println(b);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        }
    }

}
